<?php
/**
 * LowCalVanilla Functions tests
 *
 * @package Extensions
 * @subpackage test
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */




/**
 * LowCalVanilla File class tests
 * @package Extensions
 */
class TestFile extends UnitTestCase {

	var $Context;
	var $TestPath;
	var $Time;

	function setUp() {
		$this->Context = new TestContext();

		$this->TestPath = TEST_DIR . '/_files/extensions/Baz/baz.css';
		$this->Time = 1190000000;
		touch($this->TestPath, $this->Time);
	}

	function testConstructor() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertTrue($File->Path);
	}

	function testClose() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$File->Open('rb');
		$this->assertTrue(is_resource($File->_fp));
		$File->Close();
		$this->assertFalse(is_resource($File->_fp));
		$this->assertFalse($File->_fp);
	}

	function testExist() {
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/extensions/Baz/baz');
		$this->assertFalse($File->Exist());

		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertTrue($File->Exist());
	}

	function testGetContent() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual(
			'/*',
			$File->GetContent(2, false));
		$this->assertEqual(
			' test */',
			$File->GetContent(null));
		$this->assertEqual(
			'/*',
			$File->GetContent(2));
	}

	function testGetExtension(){
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual(
			'css',
			$File->GetExtension());

		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath . '.gz');

		$this->assertEqual(
			'gz',
			$File->GetExtension());
	}

	function testGetLastModification() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual($this->Time, $File->GetLastModification());
	}

	function testGetName() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual('baz.css', $File->GetName());
	}

	function testGetSize() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertEqual(10, $File->GetSize());
	}

	function testHasChanged() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$this->assertTrue($File->HasChanged($this->Time - 1));
		$this->assertFalse($File->HasChanged($this->Time));
	}

	function testOpen() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$File->Open('rb');
		$this->assertTrue(is_resource($File->_fp));
	}

	function testSetContent() {
		$this->assertFalse(file_exists(TEST_DIR . '/_files/cache/foo'));
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo');
		$this->assertEqual(16, $File->SetContent('/* test write */'));
		$File->Clear();
		unset($File);
		$this->assertTrue(file_exists(TEST_DIR . '/_files/cache/foo'));
		$this->assertEqual('/* test write */',
			file_get_contents(TEST_DIR . '/_files/cache/foo'));
		unlink(TEST_DIR . '/_files/cache/foo');

		$this->assertFalse(file_exists(TEST_DIR . '/_files/cache/foo'));
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo');

		$File->SetContent('/* test', false);
		$File->SetContent(' write */');
		clearstatcache();
		$this->assertEqual(16, $File->GetSize());
		$File->Clear();
		unset($File);
		$this->assertTrue(file_exists(TEST_DIR . '/_files/cache/foo'));
		$this->assertEqual('/* test write */',
			file_get_contents(TEST_DIR . '/_files/cache/foo'));
		unlink(TEST_DIR . '/_files/cache/foo');
	}

	function testSetPath() {
		$File = new LowCalVanilla_File(
			$this->Context,
			$this->TestPath);
		$path = array(
			$this->Context->Configuration['APPLICATION_PATH'],
			'/extensions/Baz/baz.js'
		);
		$result = $File->SetPath($path);
		$expected = $this->Context->Configuration['APPLICATION_PATH'] . '/extensions/Baz/baz.js';
		$this->assertEqual(realpath($expected), realpath($result));
	}

	function testUnlink() {
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo');
		$File->SetContent('/* test write */');
		$this->assertTrue($File->Exist());
		$File->Unlink();
		$this->assertFalse($File->Exist());
	}

	function testWritable() {
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo');
		$this->assertEqual(0, $File->Writable());
		$File = new LowCalVanilla_File(
			$this->Context,
			TEST_DIR . '/_files/cache/foo/bar');
		$this->assertEqual(-1, $File->Writable());
	}

}